package demo.playfile.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import org.cybergarage.http.HTTPRequest;
import org.cybergarage.http.HTTPResponse;
import org.cybergarage.http.HTTPServerList;
import org.cybergarage.http.HTTPStatus;

import demo.playfile.util.FileUtil;

 

 

public class HTTPRequestListenerImp   implements org.cybergarage.http.HTTPRequestListener
{
	
	public static final String CONTENT_EXPORT_URI = "/smb";
	public static final String tag = "dlna.certus.iptv.sharefile.server.ShareFileManager";
	private HTTPServerList httpServerList = new HTTPServerList();
	// Ĭ�ϵĹ���˿�
//	private int HTTPPort = 7788;
	// �󶨵�ip
	private String bindIP = null;

	
	public String getBindIP()
	{
		return bindIP;
	}

	public void setBindIP(String bindIP)
	{
		this.bindIP = bindIP;
	}

	public HTTPServerList getHttpServerList()
	{
		return httpServerList;
	}

	public void setHttpServerList(HTTPServerList httpServerList)
	{
		this.httpServerList = httpServerList;
	}

//	public int getHTTPPort()
//	{
//		return HTTPPort;
//	}
//
//	public void setHTTPPort(int hTTPPort)
//	{
//		HTTPPort = hTTPPort;
//	}
//
//	@Override
//	public void run()
//	{
//		super.run();
//
//		/**************************************************
//		 * 
//		 * ����http�����������չ�������
//		 * 
//		 *************************************************/
//		// ���Դ���
////		int retryCnt = 0;
////		// ��ȡ�˿� 2222
////		int bindPort = getHTTPPort();
////
////		HTTPServerList hsl = getHttpServerList();
////		while (hsl.open(bindPort) == false)
////		{
////			retryCnt++;
////			// ���Դ������ڷ��������Դ���ʱ����
////			if (100 < retryCnt)
////			{
////				return;
////			}
////			setHTTPPort(bindPort + 1);
////			bindPort = getHTTPPort();
////		}
////		// �������е�ÿ��HTTPServer�������HTTPRequestListener����
////		hsl.addRequestListener(this);
////		// ���ü���������HTTPServer��start����
////		hsl.start(); 
////		
////		FileUtil.ip = hsl.getHTTPServer(0).getBindAddress();
////		FileUtil.port = hsl.getHTTPServer(0).getBindPort();
//		 
//	}

	@Override
	public void httpRequestRecieve(HTTPRequest httpReq)
	{

		if (httpReq.getParameterValue("$m") != null
				&& httpReq.getParameterValue("$m").trim().length() > 0) {
			httpRequestRecieveX(httpReq);
			return;
		}
		String uri = httpReq.getURI();

		String filePaths = httpReq.getParameterValue("file");

		try
		{
			SmbFile file = new SmbFile(filePaths);
			
			// ��ȡ�ļ��Ĵ�С
			long contentLen = file.length();
			// ��ȡ�ļ�����
			String contentType = FileUtil.getFileType(filePaths);
			// ��ȡ���ļ���
			InputStream contentIn = file.getInputStream();

			if (contentLen <= 0 || contentType.length() <= 0
					|| contentIn == null)
			{
				httpReq.returnBadRequest();
				return;
			} 
		 
			
			HTTPResponse httpRes = new HTTPResponse();
			httpRes.setContentType(contentType);
			httpRes.setStatusCode(HTTPStatus.OK);
			httpRes.setContentLength(contentLen);
			httpRes.setContentInputStream(contentIn);

			httpReq.post(httpRes);

		    contentIn.close(); 
		}
		catch (MalformedURLException e)
		{
			httpReq.returnBadRequest();
			return;
		}
		catch (SmbException e)
		{
			httpReq.returnBadRequest();
			return;
		}
		catch (IOException e)
		{
			httpReq.returnBadRequest();
			return;
		}

	}

	/**?file="+URLEncoder.encode(mv,"utf-8")
	attilax    2016年4月15日  下午10:08:34
	 * @param httpReq
	 */
	private void httpRequestRecieveX(HTTPRequest httpReq) {
		String f=httpReq.getParameterValue("file");
		String filePaths = httpReq.getParameterValue("file");

		try
		{
			File file = new File(filePaths);
			
			// ��ȡ�ļ��Ĵ�С
			long contentLen = file.length();
			// ��ȡ�ļ�����
			String contentType = FileUtil.getFileType(filePaths);
			// ��ȡ���ļ���
			InputStream contentIn =new   FileInputStream(file);

			if (contentLen <= 0 || contentType.length() <= 0
					|| contentIn == null)
			{
				httpReq.returnBadRequest();
				return;
			} 
		 
			
			HTTPResponse httpRes = new HTTPResponse();
			httpRes.setContentType(contentType);
			httpRes.setStatusCode(HTTPStatus.OK);
			httpRes.setContentLength(contentLen);
			httpRes.setContentInputStream(contentIn);

			httpReq.post(httpRes);

		    contentIn.close(); 
		}
		catch (MalformedURLException e)
		{
			httpReq.returnBadRequest();
			return;
		}
		catch (SmbException e)
		{
			httpReq.returnBadRequest();
			return;
		}
		catch (IOException e)
		{
			httpReq.returnBadRequest();
			return;
		}
		
	}

}
